package com.intcast;

import java.util.Random;
import java.util.Scanner;

/*
要写注释哦!!!
*/
public class Demo10 {
    public static void main(String[] args) {
       /* int[] userBalls = userBalls();
        System.out.println(arrayToString(userBalls));
        int[] systemBalls = systemBalls();
        System.out.println(arrayToString(systemBalls));

        int money = dream(systemBalls, userBalls);
        System.out.println(money);*/

        int totalMoney = 0;
        int day = 0;
        while (true) {
            int[] systemBalls = systemBalls();
            int[] userBalls1 = systemBalls();
            int[] userBalls2 = systemBalls();
            int[] userBalls3 = systemBalls();
            int money1 = dream(systemBalls, userBalls1);
            int money2 = dream(systemBalls, userBalls2);
            int money3 = dream(systemBalls, userBalls3);
            totalMoney += money1;
            totalMoney += money2;
            totalMoney += money3;
            day++;
            if (money1 >= 5000000 || money2 >= 5000000 || money3 >= 5000000) {
                break;
            }
        }
        System.out.println("经过了" + day + "天,核算为" + (day * 1.0 / 366) + " 年, 终于中了大奖,目前总收入" + totalMoney + "元,总共花了" + day * 6 + "元");

    }

    /**
     * 作用: 开奖
     *
     * @param systemBalls 系统随机的球
     * @param userBalls   用户选择的球
     * @return 奖金
     */
    public static int dream(int[] systemBalls, int[] userBalls) {
        //定义两个变量分别记录红球中奖个数以及蓝球中奖个数
        int redCount = 0;
        int blueCount = 0;
        //遍历 userBalls 去 systemBalls查红球中奖个数
        for (int i = 0; i < userBalls.length - 1; i++) {
            int userRedBall = userBalls[i];
            //拿用户的球去systemBalls中去查
            for (int j = 0; j < systemBalls.length - 1; j++) {
                if (systemBalls[j] == userRedBall) {
                    redCount++;
                    break;
                }
            }
        }
        //算出蓝球的中奖个数
        blueCount = userBalls[userBalls.length - 1] == systemBalls[systemBalls.length - 1] ? 1 : 0;
        //对比表格进行开奖
        if (redCount + blueCount == 7) {
            return 10000000;
        } else if (redCount == 6) {
            return 5000000;
        } else if (redCount + blueCount == 6) {
            return 3000;
        } else if (redCount + blueCount == 5) {
            return 200;
        } else if (redCount + blueCount == 4) {
            return 10;
        } else if (blueCount == 1) {
            return 5;
        } else {
            return 0;
        }

    }

    /**
     * 作用: 系统随机一组双色球并返回
     *
     * @return 返回一组双色球
     */
    public static int[] systemBalls() {
        int[] systemBalls = new int[7];
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            int redBall = random.nextInt(33) + 1;
            //验证redBall在systemBalls中没有出现过才能放!!!!!!!
            int count = 0;
            //统计 redBall在systemBalls出现的次数
            for (int j = 0; j < systemBalls.length; j++) {
                if (systemBalls[j] == redBall) {
                    count++;
                    break;
                }
            }
            //走到这里判断count是否为0
            if (count == 0) {
                //证明没有出现过, 则将redBall放入到集合中
                systemBalls[i] = redBall;
            } else {
                //证明随机的出现过的, 那么我们需要再给一次机会, 下次操作的索引应该不变
                i--;
            }
        }
        //在随机一个蓝球 , 蓝球是1-16之间
        systemBalls[systemBalls.length - 1] = random.nextInt(16) + 1;
        //将这组球返回
        return systemBalls;
    }

    /**
     * 作用: 这个方法可以让用户输入一组双色球
     *
     * @return 一组双色球
     */
    public static int[] userBalls() {
        Scanner sc = new Scanner(System.in);
        //定义数组, 用来装所有的双色球
        int[] userBalls = new int[7];
        for (int i = 0; i < 6; i++) {
            //调用可以将数组变成字符串的方法,得到字符串
            String str = arrayToString(userBalls);
            System.out.println("请输入第" + (i + 1) + "个红球,红球的范围是[1~33], 红球不能重复,目前有的球是:" + str);
            userBalls[i] = sc.nextInt();
        }
        System.out.println("请输入蓝球,蓝球的范围是:1~16,目前里面球有:" + arrayToString(userBalls));
        int blueBall = sc.nextInt();
        userBalls[userBalls.length - 1] = blueBall;
        //将用户选择的这组球返回给调用处!!!!
        return userBalls;

    }

    /**
     * 作用: 将数组变成字符串返回
     *
     * @param arr 需要传入数组
     * @return 返回字符串
     */
    public static String arrayToString(int[] arr) {
        String result = "[";
        for (int i = 0; i < arr.length; i++) {
            if (i == arr.length - 1) {
                result += arr[i];
            } else {
                result += arr[i] + ",";

            }
        }
        result += "]";
        return result;
    }
}

